package com.zhuiyun.project.api.personmessage.mapper;

import com.zhuiyun.project.api.personmessage.entity.PersonMessage;
import com.zhuiyun.project.api.personmessage.model.PersonMessageModel;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @ClassName PersonMessageMapper
 * @Description TODO
 * @Author kdj
 * @Date 2023/6/27 14:28
 **/
@Mapper
public interface PersonMessageMapper extends BaseMapper<PersonMessage> {

    /**
     * @param fromId
     * @param toId
     * @Author kdj
     * @Description 查询聊天记录
     * @Date 2023/7/18 14:36
     * @Return java.util.List<com.zhuiyun.project.api.personmessage.model.PersonMessageModel>
     */
    @Select("select person_message.*,message.uuid from person_message left join message on person_message.message_id=message.id WHERE del_flag = 0" +
            "            AND ( (to_id = #{fromId} AND from_id =  #{toId})" +
            "            OR (to_id = #{toId} AND from_id = #{fromId})) order by create_time asc")
    List<PersonMessageModel> getChatRecord(int fromId, int toId);

    /**
     * @param uuid
     * @Author kdj
     * @Description 根据uuid找到该消息
     * @Date 2023/7/18 15:21
     * @Return com.zhuiyun.project.api.personmessage.entity.PersonMessage
     */
    @Select("select person_message.*,message.uuid from message,person_message WHERE person_message.message_id=message.id and  message.uuid=#{uuid} and del_flag = 0")
    PersonMessage getMsgByUuid(String uuid);

    /**
     * @param id
     * @Author kdj
     * @Description 修改阅读状态
     * @Date 2023/7/19 19:21
     * @Return int
     */
    @Update("update person_message set is_read=1 and update_time=now() where id=#{id}")
    int upIsRead(int id);


    /**
     * @param ids
     * @Author kdj
     * @Description 修改阅读状态
     * @Date 2023/7/19 19:21
     * @Return int
     */
    @Update("update person_message set is_read=1  and update_time=now() where id in #{ids} and is_withdraw=0")
    int upIsReadArr(String ids);


    /**
     * @param deleteUserId
     * @param id
     * @Author kdj
     * @Description 删除消息
     * @Date 2023/7/20 15:13
     * @Return int
     */
    @Update("UPDATE person_message SET delete_user_id = #{deleteUserId} WHERE id = #{id};")
    int upDelMsg(String deleteUserId, int id);
    /**
     * @param fromId
     * @param toId
     * @Author kdj
     * @Description 查询聊天记录
     * @Date 2023/7/18 14:36
     * @Return java.util.List<com.zhuiyun.project.api.personmessage.model.PersonMessageModel>
     */
    @Select("select person_message.*,message.uuid from person_message left join message on person_message.message_id=message.id WHERE del_flag = 0" +
            "            AND ( (to_id = #{fromId} AND from_id =  #{toId})" +
            "            OR (to_id = #{toId} AND from_id = #{fromId})) order by create_time desc limit #{open},#{size}")
    List<PersonMessageModel> getChatRecordLimit(int fromId, int toId,int open,int size);
}
